package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.model.AccessToken;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.AccessTokenService;
import com.example.demo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserRepository ur;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AccessTokenService accessTokenService;
	
	@GetMapping("/info")
	@ResponseBody
	public Object info(HttpServletRequest request) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			String token = request.getHeader("X-Token");
			System.out.println("user/info获取到的token是 ： "+token);
			//根据token获取用户信息
			User user = new User();
			AccessToken accessToken = accessTokenService.findByToken(token);
			if(accessToken != null && !StringUtils.isEmpty(accessToken.getAccessToken())) {
				user = userService.findByUsername(accessToken.getUsername());
			}
			//role
			List<String> roles  = new ArrayList<String>();
			Map userMap = new HashMap<String, String>();
			roles.add("admin");
			userMap.put("roles", roles);
			userMap.put("introduction", "都是测试的数据");
			userMap.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
			userMap.put("name", user.getRealname());
			result.put("data", userMap);
		} catch (Exception e) {
			result.put("code", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
		/*
		 格式
		 {
		 	"code":20000,
		 	"data":{
		 		"roles":["admin"],
		 		"introduction":"I am a super administrator",
		 		"avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
		 		"name":"Super Admin"
		 	}
	 	}
		 */
//		return "{ \"code\": 20000, \"data\": { \"token\": \"admin-token\" } }";
//		System.out.println(result);
		return result;
	}
	
	@PostMapping("/register")
	@ResponseBody
	public Object register(@RequestBody User user) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			userService.register(user);
			System.out.println("注册账号："+user.getRealname());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
		return result;
	}
//	@RequestMapping(value="list",method = RequestMethod.GET )
	@GetMapping("list")
	@ResponseBody
	public Object list(
			@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum, 
			@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
			User user,HttpServletRequest request) {
		System.out.println("user/list");
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
//			List<User> entityList = new ArrayList<User>();
			user.setIsDelete("0");
			ExampleMatcher matcher = ExampleMatcher.matching()
				.withMatcher("isDelete", ExampleMatcher.GenericPropertyMatchers.ignoreCase())
				.withMatcher("realname", ExampleMatcher.GenericPropertyMatchers.contains())
				.withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
				.withMatcher("phone", ExampleMatcher.GenericPropertyMatchers.ignoreCase());
			Example<User> example = Example.of(user,matcher);
			PageRequest pageable = PageRequest.of((pageNum-1), pageSize);
			Page<User> pageResult = userService.findAll(example,pageable);
//			entityList = pageResult.getContent();
			result.put("message", pageResult);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("message", "请求失败"+e.getMessage());
		}
		return result;
	}
	
	@PostMapping("saveOrUpdate")
	@ResponseBody
	public Object saveOrUpdate(User entity) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			userService.saveOrUpdate(entity);
		} catch (Exception e) {
			result.put("code", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
		return result;
	}
	
	@GetMapping("delete")
	@ResponseBody
	public Object delete(@RequestParam(value = "id",required = false) String id) {
		System.out.println("user/delete请求... id="+id);
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			userService.deleteLogic(id);
		} catch (Exception e) {
			result.put("code", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
		return result;
	}
}
